//
//  LJLRecommendedVC.m
//  聆听FM
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 Dark. All rights reserved.
//
#import "LTNetTool.h"

#import "LJLRecommendedVC.h"
#import "LJLRecommendedModel.h"
#import "LJLRecommendedTVC.h"
#import "AlbumDetailViewController.h"
#import "CBStoreHouseRefreshControl.h"

@interface LJLRecommendedVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *recommendedTV;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) CBStoreHouseRefreshControl *storeHouseRefreshControl;

@property (nonatomic, assign) NSInteger page;
@end

@implementation LJLRecommendedVC
- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
    [self createView];
    self.page = 1;
    [self createDataWithPage:self.page];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}

- (void)createView
{
    self.recommendedTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 113) style:UITableViewStylePlain];
    self.recommendedTV.delegate = self;
    self.recommendedTV.dataSource = self;
    self.recommendedTV.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_recommendedTV];
    self.storeHouseRefreshControl = [CBStoreHouseRefreshControl attachToScrollView:self.recommendedTV target:self refreshAction:@selector(refreshTriggered:) plist:@"LTFM" color:[UIColor whiteColor] lineWidth:3 dropHeight:80 scale:1.2 horizontalRandomness:300 reverseLoadingAnimation:NO internalAnimationFactor:0.7];
    
    //加载
    self.recommendedTV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self getMoreData];
    }];
    
    [self.recommendedTV registerClass:[LJLRecommendedTVC class] forCellReuseIdentifier:@"LJLrecommendedTVC"];
}
- (void)getMoreData
{
    self.page++;
    [self createDataWithPage:self.page];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.storeHouseRefreshControl scrollViewDidScrollWithType:0];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self.storeHouseRefreshControl scrollViewDidEndDragging];
}

- (void)refreshTriggered:(id)sender
{
    
    [self performSelector:@selector(finishRefreshControl) withObject:nil afterDelay:2 inModes:@[NSRunLoopCommonModes]];
}

- (void)finishRefreshControl
{
    [self getData];
}

- (void)getData
{
    [self.dataSource removeAllObjects];
    [self.recommendedTV reloadData];
    self.page = 1;
    [self createDataWithPage:self.page];
   
}
- (void)createDataWithPage:(NSInteger)page
{
    [LTNetTool GetNetWithURL:@"http://mobile.ximalaya.com/feed/v1/recommend/classic?device=android&pageId=1&pageSize=20&statEvent=pageview%2Ftab%40%E8%AE%A2%E9%98%85%E5%90%AC_%E6%8E%A8%E8%8D%90" Body:nil Header:nil Response:0 Success:^(id result) {
        NSArray *array = [[result objectForKey:@"data"] objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            LJLRecommendedModel *model = [[LJLRecommendedModel alloc] initWithDic:dic];
            [self.dataSource addObject:model];
        }
        [_storeHouseRefreshControl finishingLoading];
        [_recommendedTV.mj_footer endRefreshing];
        [self.recommendedTV reloadData];
    } Failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * FITHEIGHT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJLRecommendedTVC *cell = [tableView dequeueReusableCellWithIdentifier:@"LJLrecommendedTVC"];
    cell.model = [self.dataSource objectAtIndex:indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AlbumDetailViewController *rdvc = [[AlbumDetailViewController alloc] init];
    LJLRecommendedModel *model = [self.dataSource objectAtIndex:indexPath.row];
    rdvc.albmId = model.albumId;
    rdvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:rdvc animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
